#!/usr/bin/python
# coding=UTF-8
import cv2
import config
import multiprocessing
from lock import *  # 进程间共享停止位
import logging
class CameraClass:
    def __init__(self, lock, src=0, shape=[480, 320]):
        self.src = src
        self.shape = shape
        self.lock = lock
        self.first = True

    def start(self):
        """
        摄像头的一个线程，图片供巡航和检测用
        :return:
        """
        if self.first:
            if self.src == 0:
                STOPPED_cam_front.value = False
            elif self.src == 1:
                STOPPED_cam_right.value = False
            else:
                STOPPED_cam_left.value = False
            
            if self.src == 0:
                multiprocessing.Process(target = self.update).start()
            elif self.src == 1:
                multiprocessing.Process(target = self.update).start()
            else:
                multiprocessing.Process(target = self.update).start()
            
            self.first = False

        else:
            if self.src == 0:
                STOPPED_cam_front.value = False
            elif self.src == 1:
                STOPPED_cam_right.value = False
            else:
                STOPPED_cam_left.value = False
        
    def update(self):
        """
        如果stopped的话，就停止读图片
        :return:
        """

        """
        因为进程的原因 摄像头初始化挪到这里
        """

        stream = cv2.VideoCapture(self.src)
        logging.debug('camera stream is opened?：{}'.format(stream.isOpened()))

        if not stream.isOpened():
            stream.release()
            stream = cv2.VideoCapture(self.src)

        stream.set(cv2.CAP_PROP_FRAME_WIDTH, self.shape[0])
        stream.set(cv2.CAP_PROP_FRAME_HEIGHT, self.shape[1])

        for _ in range(5):
            (self.grabbed, self.frame) = stream.read() # 摄像头暖车

        while True:
            i = 0
            i +=1
            if self.src == 0:
                if STOPPED_cam_front.value:
                    pass
                else:
                    (self.grabbed, self.frame) = stream.read()
                    if self.grabbed:
                        if not (i%10):
                            logging.debug("front_cam working")
                        self.lock.acquire()  # 上锁
                        cv2.imwrite("0.jpg", self.frame)  # 写到本地
                        self.lock.release()  # 开锁


            elif self.src == 1:
                if STOPPED_cam_right.value:
                    pass
                else:
                    (self.grabbed, self.frame) = stream.read()
                    if self.grabbed:
                        self.lock.acquire()  # 上锁
                        cv2.imwrite("0.jpg", self.frame)  # 写到本地
                        self.lock.release()  # 开锁

            else:
                if STOPPED_cam_left.value:
                    pass
                else:
                    (self.grabbed, self.frame) = stream.read()
                    if self.grabbed:
                        self.lock.acquire()  # 上锁
                        cv2.imwrite("0.jpg", self.frame)  # 写到本地
                        self.lock.release()  # 开锁
            
    def read(self):
        self.lock.acquire()  # 上锁
        img = cv2.imread("0.jpg")  # 从本地读
        self.lock.release()  # 开锁
        return img

    def stop(self):
        if self.src == 0:
            self.lock.acquire()
            STOPPED_cam_front.value = True
            self.lock.release()

        elif self.src == 1:
            self.lock.acquire()
            STOPPED_cam_right.value =  True
            self.lock.release()

        else:
            self.lock.acquire()
            STOPPED_cam_left.value =  True
            self.lock.release()

right_cam_Object = CameraClass(locklock, config.right_cam, [320, 240])
left_cam_Object = CameraClass(locklock2, config.left_cam, [320, 240])
front_cam_Object = CameraClass(locklock3, config.front_cam, [320, 240])

"""
camera测试代码
"""

# if __name__ == "__main__":
#     front_cam_Object.start()
#     while True:
#         try:
#             img = front_cam_Object.read()
#             imgresize = cv2.resize(img, (100,100))
           
#             cv2.imshow('a',imgresize)
#             cv2.waitKey(2)
#         except Exception as e:

#             logging.error(e)
#             pass
        
